﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utility
{
    public partial class Param
    {
        /// <summary>
        /// 值Key @name
        /// </summary>
        private object valueParam;


        //  private string _keyParam;
        /// <summary>
        /// 键Key @name
        /// </summary>
        public string KeyParam
        {
            get;
            set;
            //get
            //{
            //    return _keyParam;
            //}
            //set
            //{
            //    this._keyParam = "@" + value;
            //}
        }

        public string SqlParam
        {
            get
            {
                return "@" + KeyParam;
            }
        }
        /// <summary>
        /// 具体值
        /// </summary>
        public object ValueParam
        {
            get
            {
                return this.valueParam;
            }
            set
            {
                if (value == null)
                {
                    this.valueParam = System.DBNull.Value;
                }
                else
                {
                    this.valueParam = value;
                }
            }
        }
        /// <summary>
        /// 对应数据类型
        /// </summary>
        public DbType ParamType
        {
            get;
            set;
        }
        public ParameterDirection InOutType
        {
            get;
            set;
        }
        public object ReturnValue
        {
            get;
            set;
        }
        public int Size
        {
            get;
            set;
        }

        /// <summary>
        /// 公用数据库参数
        /// </summary>
        /// <param name="keyParam">ParameterName 参数名称</param>
        /// <param name="valueParam">Value 值</param>
        public Param(string keyParam, object valueParam)
        {
            this.ParamType = DbType.String;
            this.Size = 100;
            this.KeyParam = keyParam;
            this.ValueParam = valueParam;
        }

        /// <summary>
        /// 公用数据库参数
        /// </summary>
        /// <param name="keyParam">ParameterName 参数名称</param>
        /// <param name="valueParam">Value 值</param>
        /// <param name="paramType">DbType 参数类型 </param> 
        public Param(string keyParam, object valueParam, DbType paramType)
        {
            this.KeyParam = keyParam;
            this.ValueParam = valueParam;
            this.ParamType = paramType;
            this.Size = 100;
        }

        /// <summary>
        /// 公用数据库参数
        /// </summary>
        /// <param name="keyParam">ParameterName 参数名称</param>
        /// <param name="valueParam">Value 值</param>
        /// <param name="paramType">DbType 参数类型 </param> 
        /// <param name="inOutType">参数出入类型</param>
        /// <param name="size">参数长度</param>
        public Param(string keyParam, object valueParam, DbType paramType, ParameterDirection inOutType, int size)
        {
            this.KeyParam = keyParam;
            this.ValueParam = valueParam;
            this.ParamType = paramType;
            this.InOutType = inOutType;
            this.ReturnValue = null;
            this.Size = size;
        }

        /// <summary>
        /// keyParam
        /// </summary>
        /// <returns></returns>
        public string ReturnKeyParam(string keyParam)
        {
            return "@" + keyParam;
        }

        /// <summary>
        /// 得到keyParam 对应的value值
        /// </summary>
        /// <param name="parmas">Param 数组</param>
        /// <param name="keyParam">keyParam</param>
        /// <returns></returns>
        public static object GetValueByKey(Param[] parmas, string keyParam)
        {
            object result;
            for (int i = 0; i < parmas.Length; i++)
            {
                Param parma = parmas[i];
                if (parma.KeyParam == keyParam)
                {
                    result = parma.ValueParam;
                    return result;
                }
            }
            result = null;
            return result;
        }


    }
}
